package top.infopub.mgc.common;


import java.util.Date;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import top.infopub.concurrent.ExecutorManager;
import top.infopub.model.sys.ErrorLog;
import top.infopub.tdp.util.ErrorLogUtil;
import top.infopub.zookeeper.confclient.ContextUtils;


/**
 * error异常日志入表 service层
 * @author Awoke
 * @version 2018年9月27日
 * @see ErrorLogAop
 * @since
 */
@Component
@Aspect
public class ErrorLogAop {

    private static final Logger logger = LoggerFactory.getLogger(ErrorLogAop.class);

    @AfterThrowing(pointcut = "execution (* top.infopub..*.service.impl.*Impl.*(..))", throwing = "ex")
    public void afterThrowing(Throwable ex) {
        final ErrorLog log = new ErrorLog();
        log.setSprojectname(ContextUtils.PROJECT_NAME);
        log.setSerrorname(ex.getClass().getName());
        log.setSerrorinfo(ex.getMessage());
        log.setToperateDate(new Date());
        logger.error("service error--", ex);
        // 确保insert-log不被外部事务回滚掉
        ExecutorManager.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                ErrorLogUtil.log(log);
            }
        });
    }

}
